//
//  CameraDetailHeaderView.m
//  Runone
//
//  Created by runone02 on 16/11/2.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "CameraDetailHeaderView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Define.h"
#import "AppConstants.h"
#import "AppDelegate.h"
#import "Masonry.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"
#import "ROPointAnnotation.h"
#import "OneConst.h"
#import "UIImageView+WebCache.h"
#import "AppLocalManager.h"
#import "MJExtension.h"

@interface CameraDetailHeaderView ()<MAMapViewDelegate>

// ******************************* - 地图 - *******************************
@property (nonatomic, strong) UIView *mapBackView;
@property (nonatomic, strong) MAMapView *mapView;

// ******************************* - 桩号、方向- *******************************

@property (nonatomic, strong) UIView  *topView;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UILabel *pileLabel;
@property (nonatomic, strong) UILabel *derectionLabel;

// ******************************* -  - *******************************
@property (nonatomic, strong) NSMutableArray *annotaionArray;

@property (nonatomic, strong) DeviceBaseInfo *currentDeviceModel;
@end


@implementation CameraDetailHeaderView

- (void) setNewData:(DeviceBaseInfo *)baseModel LastBtnTitle:(DeviceBaseInfo *)last NextBtnTitle:(DeviceBaseInfo *)next Model:(HighwayMergeRoadRecordModel *)recordModel{
    _currentDeviceModel = baseModel;
    
    
    if (last == nil && next == nil) {
        _lastBtn.hidden = YES;
        _nextBtn.hidden = YES;
    }
    
    if (last  && next == nil ) {
        _lastBtn.hidden = NO;
        _nextBtn.hidden = YES;
        
        if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            [_lastBtn setTitle:[NSString stringWithFormat:@"%@+%ld",last.PileNo,(long)last.PileDistance] forState:UIControlStateNormal];
        }else {
            NSString *LStr    = @"L";
            NSString *LKStr   = [LStr stringByAppendingString:last.PileNo];
            [_lastBtn setTitle:[NSString stringWithFormat:@"%@+%ld",LKStr,(long)last.PileDistance] forState:UIControlStateNormal];
        }
    }
    
    if (next && last == nil ) {
        _lastBtn.hidden = YES;
        _nextBtn.hidden = NO;
        
        if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            [_nextBtn setTitle:[NSString stringWithFormat:@"%@+%ld",next.PileNo,(long)next.PileDistance] forState:UIControlStateNormal];
        }else {
            NSString *LStr    = @"L";
            NSString *LKStr   = [LStr stringByAppendingString:next.PileNo];
            [_nextBtn setTitle:[NSString stringWithFormat:@"%@+%ld",LKStr,(long)next.PileDistance] forState:UIControlStateNormal];
        }
    }
    if (last && next) {
        _lastBtn.hidden = NO;
        _nextBtn.hidden = NO;
        if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            [_lastBtn setTitle:[NSString stringWithFormat:@"%@+%ld",last.PileNo,(long)last.PileDistance] forState:UIControlStateNormal];
        }else {
            NSString *LStr    = @"L";
            NSString *LKStr   = [LStr stringByAppendingString:last.PileNo];
            [_lastBtn setTitle:[NSString stringWithFormat:@"%@+%ld",LKStr,(long)last.PileDistance] forState:UIControlStateNormal];
        }
        
        if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            [_nextBtn setTitle:[NSString stringWithFormat:@"%@+%ld",next.PileNo,(long)next.PileDistance] forState:UIControlStateNormal];
        }else {
            NSString *LStr    = @"L";
            NSString *LKStr   = [LStr stringByAppendingString:next.PileNo];
            [_nextBtn setTitle:[NSString stringWithFormat:@"%@+%ld",LKStr,(long)next.PileDistance] forState:UIControlStateNormal];
        }
    }
    
    //广乐特殊的要求，只能写死
    if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) { //主线
        _pileLabel.text      = [NSString stringWithFormat:@"%@+%ld",baseModel.PileNo, (long)baseModel.PileDistance];
    } else {
        NSString *LStr    = @"L";
        NSString *LKStr   = [LStr stringByAppendingString:baseModel.PileNo];
        _pileLabel.text   = [NSString stringWithFormat:@"%@+%ld",LKStr,(long)baseModel.PileDistance];
    }
    if (_fromStationName) {
        _derectionLabel.text = baseModel.DeviceName ;
        _pileLabel.text      = _fromStationName;
    }else {
        _derectionLabel.text = baseModel.DirectionName;
    }
    
    //_RoadPicUrl	__NSCFString *	@"http://192.168.1.200:8090/api/DownloadResource?systemCode=GuangleHighway&resType=1&resName=广乐高速.png&extra=主线"	0x00006000001c7cb0
    NSString *str=@"Bearer ";
    NSString *strone;
    SDWebImageDownloader *sdmanager = [SDWebImageManager sharedManager].imageDownloader;
    
    if (GETACCESS_TOKEN!=nil) {
        NSLog(@"%@",GETACCESS_TOKEN);
        strone=[str stringByAppendingString:GETACCESS_TOKEN];
        [sdmanager setValue:strone forHTTPHeaderField:@"Authorization"];
    }
    
    NSString * picString = recordModel.RoadPicUrl;
    [_imageV sd_setImageWithURL:[picString mj_url]];
    
    [self loadAnnotation];
}


- (void) trafficBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (btn.selected) {
        _mapView.showTraffic=YES;
    } else {
        _mapView.showTraffic=NO;
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:235.0 / 255 green:235.0 / 255 blue:240.0 / 255 alpha:1];
        _annotaionArray = [[NSMutableArray alloc] init];
        [self configUI];
        [self layoutUI];
        
    }
    return self;
}

- (void) configUI {
    
    _mapBackView = [[UIView alloc] init];
    [self addSubview:_mapBackView];
    
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    _mapView                = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    _mapView.delegate       = self;
    _mapView.showsCompass   = NO;
    _mapView.showsScale     = NO;
    _mapView.rotationDegree = 90;
    [_mapBackView addSubview:_mapView];
    
    _enlargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_enlargeBtn setBackgroundImage:[UIImage imageNamed:@"map_enlarge"] forState:UIControlStateNormal];
    [_mapBackView addSubview:_enlargeBtn];
    
    _trafficBtn                 = [UIButton buttonWithType:UIButtonTypeCustom];
    _trafficBtn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [_trafficBtn setBackgroundImage:[UIImage imageNamed:@"traffic_light"] forState:UIControlStateNormal];
    [_trafficBtn setBackgroundImage:[UIImage imageNamed:@"traffic_light_selected"] forState:UIControlStateSelected];
    [_trafficBtn addTarget:self action:@selector(trafficBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_mapBackView addSubview:_trafficBtn];
    
    _lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _lastBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _lastBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    [_lastBtn setBackgroundImage:[UIImage imageNamed:@"camera_gun_btn_bg"] forState:UIControlStateNormal];
    [_mapBackView addSubview:_lastBtn];
    
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _nextBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    [_nextBtn setBackgroundImage:[UIImage imageNamed:@"camera_gun_btn_bg"] forState:UIControlStateNormal];
    [_mapBackView addSubview:_nextBtn];
    
    _topView = [[UIView alloc] init];
    _topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_topView];
    
    _pileLabel = [[UILabel alloc] init];
    _pileLabel.font = [UIFont systemFontOfSize:15];
    [_topView addSubview:_pileLabel];
    
    _derectionLabel = [[UILabel alloc] init];
    _derectionLabel.font = [UIFont systemFontOfSize:15];
    [_topView addSubview:_derectionLabel];
    
    _imageV = [[UIImageView alloc] init];
    
    [_topView addSubview:_imageV];
}

- (void) layoutUI {
    __weak __typeof(self)weakSelf = self;
    // 地图
    [_mapBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(200);
    }];
    
    [_mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mapBackView.mas_top);
        make.left.mas_equalTo(weakSelf.mapBackView.mas_left);
        make.right.mas_equalTo(weakSelf.mapBackView.mas_right);
        make.height.mas_equalTo(200);
    }];
    
    [_enlargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mapBackView.mas_top).with.offset(5);
        make.right.mas_equalTo(weakSelf.mas_right).with.offset(-20);
        make.height.mas_equalTo(SecondMapBtnWH);
        make.width.mas_equalTo(SecondMapBtnWH);
    }];
    
    [_trafficBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.enlargeBtn.mas_bottom).with.offset(ButtunMargin);
        make.right.mas_equalTo(weakSelf.mapBackView.mas_right).with.offset(-20);
        make.height.mas_equalTo(SecondMapBtnWH);
        make.width.mas_equalTo(SecondMapBtnWH);
    }];
    
    [_lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.mapBackView.mas_bottom).with.offset(-5);
        make.left.mas_equalTo(weakSelf.mapBackView.mas_left).with.offset(15);
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(30);
    }];
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.mapBackView.mas_bottom).with.offset(-5);
        make.right.mas_equalTo(weakSelf.mapBackView.mas_right).with.offset(-15);
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(30);
    }];
    
    // ********************
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mapBackView.mas_bottom).with.offset(8);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(50);
    }];
    
    [_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.topView.mas_centerY);
        make.left.mas_equalTo(weakSelf.mas_left).with.offset(15);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(44);
    }];
    
    [_pileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.imageV.mas_top);
        make.left.mas_equalTo(weakSelf.imageV.mas_right).with.offset(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(150);
    }];
    
    [_derectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.imageV.mas_bottom);
        make.left.mas_equalTo(weakSelf.imageV.mas_right).with.offset(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(150);
    }];
    
}
- (void)setLastStr:(NSString *)lastStr {
    _lastStr = lastStr;
    if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
        [_lastBtn setTitle:_lastStr forState:UIControlStateNormal];
    }else {
        NSString *LStr    = @"L";
        NSString *LKStr   = [LStr stringByAppendingString:_lastStr];
        [_lastBtn setTitle:LKStr forState:UIControlStateNormal];
    }
}

- (void) setNextStr:(NSString *)nextStr {
    _nextStr = nextStr;
    if ([_currentDeviceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
        [_nextBtn setTitle:_nextStr forState:UIControlStateNormal];
    }else {
        NSString *LStr    = @"L";
        NSString *LKStr   = [LStr stringByAppendingString:_lastStr];
        [_nextBtn setTitle:LKStr forState:UIControlStateNormal];
    }
    
}

- (void) loadAnnotation { //0.010089--0.020921--15.000000--25.295090--112.977897
    [_mapView removeAnnotations:_annotaionArray];
    [_annotaionArray removeAllObjects];
    ROPointAnnotation *roAnnotation = [[ROPointAnnotation alloc] init];
    float latitude  = _currentDeviceModel.Latitude;
    float longitude = _currentDeviceModel.Longitude;
    roAnnotation.coordinate  = CLLocationCoordinate2DMake(latitude, longitude);
    //    roAnnotation.coordinate    = ret;
    [_annotaionArray addObject:roAnnotation];
    CLLocationCoordinate2D centerCoordinate  = CLLocationCoordinate2DMake(latitude, longitude);
    MACoordinateSpan  span                  = MACoordinateSpanMake(0.010089, 0.020921);
    MACoordinateRegion region                = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region                          = region;
    [_mapView setCenterCoordinate:centerCoordinate animated:NO];
    [_mapView addAnnotations:_annotaionArray];
}
#pragma  mark - 地图代理
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.image = [UIImage imageNamed:@"map_camera_gun"];
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height / 2);
        return annotationView;
    }
    return nil;
}
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    CustomAnnotationView *customView = (CustomAnnotationView *)view;
    customView.selected              =  NO;
}

-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"%f--%f--%f--%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta,mapView.zoomLevel,mapView.region.center.latitude,mapView.region.center.longitude);
}

@end
